package com.db.utils;

import com.alibaba.druid.pool.DruidDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

/**
 * @author xin.ding
 * @date 2020/8/18 18:34
 */
public class DruidUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(DruidUtils.class);

    private static Properties properties;
    private static DruidDataSource connPool;

    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    private static int initSize;
    private static int minSize;
    private static int maxSize;
    private static long maxWait;
    private static long timeBetween;

    private static String validSql;

    private static boolean testWhileidle;
    private static boolean testOnBorrow;
    private static boolean testOnReturn;
    private static boolean removeAbandoned;


    private DruidUtils() {
        throw new IllegalAccessError();
    }

    static {
        properties = new Properties();
//        File file = new File("db.properties");

        File file = new File("D:/code/project/dfzq-dx/kingstar-stream-dfzq/oracle-test/src/main/resources/db.properties");
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            properties.load(fis);
            fis.close();

            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
            initSize = Integer.parseInt(properties.getProperty("initSize"));
            minSize = Integer.parseInt(properties.getProperty("minSize"));
            maxSize = Integer.parseInt(properties.getProperty("maxSize"));
            maxWait = Long.parseLong(properties.getProperty("maxWait"));
            timeBetween = Long.parseLong(properties.getProperty("timeBetween"));

            validSql = properties.getProperty("validSql");
            testWhileidle = Boolean.parseBoolean(properties.getProperty("testWhileidle"));
            testOnBorrow = Boolean.parseBoolean(properties.getProperty("testOnBorrow"));
            testOnReturn = Boolean.parseBoolean(properties.getProperty("testOnReturn"));
            removeAbandoned = Boolean.parseBoolean(properties.getProperty("removeAbandoned"));

            LOGGER.info("connect info :user[{}],pass[{}],url[{}],initSize[{}],minSize[{}],maxSize[{}],maxWait[{}],validSql[{}],removeAbandoned[{}]", username, password, url, initSize, minSize, maxSize, maxWait, validSql, removeAbandoned);

        } catch (Exception e) {
            LOGGER.error("read db.properties error", e);
        }
    }

    public static DruidDataSource getDruidDataSource() {
        if (connPool != null) {
            return connPool;
        }
        connPool = new DruidDataSource();
        connPool.setDriverClassName(driver);
        connPool.setUsername(username);
        connPool.setPassword(password);
        connPool.setUrl(url);
        connPool.setInitialSize(initSize);
        connPool.setMinIdle(minSize);
        connPool.setRemoveAbandoned(false);
        connPool.setRemoveAbandonedTimeout(30);
        connPool.setMaxWait(maxWait);
        connPool.setTimeBetweenEvictionRunsMillis(timeBetween);
        connPool.setValidationQuery(validSql);
        connPool.setTestWhileIdle(testWhileidle);
        connPool.setTestOnBorrow(testOnBorrow);
        connPool.setTestOnReturn(testOnReturn);
        return connPool;
    }

    public static DruidDataSource testDruid() {
        if (connPool != null) {
            return connPool;
        }
        return getDruidDataSource(driver, username, password, url, initSize, maxSize, validSql);
    }

    public static DruidDataSource getDruidDataSource(String driver, String user, String password, String url, int poolInitSize, int poolMaxSize, String validationQuery) {
        connPool = new DruidDataSource();
        connPool.setDriverClassName(driver);
        connPool.setUsername(user);
        connPool.setPassword(password);
        connPool.setUrl(url);
        connPool.setInitialSize(poolInitSize);
        connPool.setMinIdle(1);
        connPool.setMaxActive(poolMaxSize);
        connPool.setRemoveAbandoned(false);
        connPool.setRemoveAbandonedTimeout(30);
        connPool.setMaxWait(20000L);
        connPool.setTimeBetweenEvictionRunsMillis(20000L);
        connPool.setValidationQuery(validationQuery);
        connPool.setTestWhileIdle(true);
        connPool.setTestOnBorrow(true);
        return connPool;
    }

    public static void close() {
        if (connPool != null) {
            connPool.close();
        }
    }
}
